﻿Imports System.Data.OleDb
Imports System.Configuration

Public Class TipoProducto
    Public connDB As OleDb.OleDbConnection
    Dim cmd As OleDb.OleDbCommand

    Private Sub TipoProducto_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        listar()
        listar_combo()
    End Sub

    Public Sub conexion()
        Try
            Dim conString As String = ConfigurationManager.ConnectionStrings("conDB").ConnectionString
            connDB = New OleDb.OleDbConnection(conString)
            'MsgBox("conextado corextamente")
        Catch ea As Exception
            MsgBox(ea.Message)
            connDB.Close()
        End Try
    End Sub

    Public Sub listar()
        conexion()
        Dim MiDataSet As New DataSet
        Dim MiAdaptador As New OleDb.OleDbDataAdapter
        Try
            connDB.Open()
            Dim sql As String = "SELECT tp.Id_tipo as [Id Tipo], tp.Descripcion as [Descripcion Tipo], t.Id_turno as [Id Turno], t.Descripcion as [Descripcion Turno] FROM Tipo_producto tp, Turno t where tp.id_turno=t.id_turno"

            Dim comando As New OleDbCommand(sql, connDB)
            MiAdaptador.SelectCommand = comando
            MiAdaptador.Fill(MiDataSet)
            DataGridView1.DataSource = MiDataSet.Tables(0)
        Catch ex As Exception
            MessageBox.Show("error " & Err.Number.ToString & vbCrLf & ex.Message)
            connDB.Close()
        End Try
    End Sub

    Public Sub listar_combo()
        conexion()
        Dim MiDataSet As New DataSet
        Dim MiAdaptador As New OleDb.OleDbDataAdapter
        Try
            connDB.Open()
            Dim sql As String = "SELECT Id_turno, [descripcion] FROM Turno "

            Dim comando As New OleDbCommand(sql, connDB)
            MiAdaptador.SelectCommand = comando
            MiAdaptador.Fill(MiDataSet)

            ComboBox1.DataSource = MiDataSet.Tables(0)
            ComboBox1.DisplayMember = MiDataSet.Tables(0).Columns(1).Caption.ToString()
            ComboBox1.ValueMember = MiDataSet.Tables(0).Columns(0).Caption.ToString()
        Catch ex As Exception
            MessageBox.Show("error " & Err.Number.ToString & vbCrLf & ex.Message)
            connDB.Close()
        End Try
    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim Id_tipo As Integer = ComboBox1.SelectedValue
        Try
            conexion()
            connDB.Open()
            cmd = New OleDb.OleDbCommand("INSERT INTO tipo_Producto(Descripcion, Id_turno) values ('" & TextBox1.Text & "', " & Id_tipo & ")", connDB)
            cmd.ExecuteNonQuery()
            connDB.Close()
            MsgBox("Tipo de Producto Registrado Exitosamente")
            listar()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim Id_tipo As Integer = ComboBox1.SelectedValue
        Try
            conexion()
            connDB.Open()
            cmd = New OleDb.OleDbCommand("UPDATE tipo_Producto SET Descripcion = '" & TextBox1.Text & "', Id_turno =" & Id_tipo & " WHERE Id_tipo = " & TextBox2.Text, connDB)
            cmd.ExecuteNonQuery()
            connDB.Close()
            MsgBox("La Actualización del Tipo de Producto fue Exitosa")
            listar()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Try
            conexion()
            connDB.Open()
            cmd = New OleDb.OleDbCommand("DELETE FROM tipo_Producto WHERE Id_tipo = " & TextBox2.Text, connDB)
            cmd.ExecuteNonQuery()
            connDB.Close()
            MsgBox("El Tipo de Producto fue Eliminado Exitosamente")
            listar()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub DataGridView1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGridView1.SelectionChanged
        TextBox1.Text = Me.DataGridView1.Rows(DataGridView1.CurrentRow.Index).Cells(1).Value
        TextBox2.Text = Me.DataGridView1.Rows(DataGridView1.CurrentRow.Index).Cells(0).Value
        ComboBox1.SelectedValue = Me.DataGridView1.Rows(DataGridView1.CurrentRow.Index).Cells(2).Value
    End Sub
End Class